// pages/fans/fans.js
const app = getApp()
var server = require('../../utils/server');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tu: app.globalData.tupian,
    dj: 1,
    care: [],
    carecount: 0,
    fans: [],
    fanscount: 0,
    regex: '^',//搜索
    hasNext:false,
  },

  onLoad: function(e) {
    that = this
    console.log('dj', e)
    that.user(e.dj,'^')
    that.setData({
      dj: e.dj,
      fanscount: e.fanscount,
      carecount: e.carecount
    })
  },
  user: function (dj, regex) {
    server.requestForm(false, 'get', 'user', {
        filter: {
          _id: app.globalData.userID
        }
      },
      function(e) {
        console.log(e)
        if (dj == 1) {
          server.requestForm(false, 'get', 'user/getCareList', {
              page: ({
                size: 10000,
                index: 1
              }),
              filter: {
                _id: {
                  "$in": e.data[0].careID
                },
                nickname: {
                  "$regex": regex
                }
              }
            },
            function(res) {
              console.log('res', res)
              if(res.code == 0){
                if (res.data.items) {
                  
                  for (let i = 0; i < e.data[0].careID.length; i++) {
                    for (let g = 0; g < res.data.items.length; g++) {
                      // res.items[g].gz = 0
                      if (e.data[0].careID[i] === res.data.items[g]._id) {
                        console.log('123', e.data[0].careID[i], res.data.items[g]._id)
                        res.data.items[g].gz = 1
                        console.log('gz', res.data.items[g].gz)
                      }
                    }
                  }
                  that.setData({
                    care: res.data.items,
                    carecount: res.data.count
                  })
                } else {
                  that.setData({
                    care: [],
                  })
                }
              }else{
                wx.showToast({
                  title: res.message,
                  icon: 'none',
                  duration: 1000,
                });
              }

            })
        } else {
          server.requestForm(false, 'get', 'user/getFansList', {
              page: ({
                size: 10000,
                index: 1
              }),
              filter: {
                _id: {
                  "$in": e.data[0].fansID
                },
              }
            },
            function(res) {
              if(res.code == 0){
                for (let i = 0; i < e.data[0].careID.length; i++) {
                  for (let g = 0; g < res.data.items.length; g++) {
                    // res.items[g].gz = 0
                    if (e.data[0].careID[i] === res.data.items[g]._id) {
                      console.log('123', e.data[0].careID[i], res.data.items[g]._id)
                      res.data.items[g].gz = 1
                      console.log('gz', res.data.items[g].gz)
                    }
                  }
                }
                that.setData({
                  fans: res.data.items,
                  fanscount: res.data.count,
                  hasNext:res.data.hasNext
                })
              }else{
                wx.showToast({
                  title: res.message,
                  icon: 'none',
                  duration: 1000,
                });
              }

            })
        }
      })
  },
  ss:function(e){
    console.log(e)
    var regex = e.detail.value
    if (regex==''){
      that.setData({
        regex:'^'
      })
    }else{
      that.setData({
        regex: regex
      })
    }
  },
  sous:function(e){
    that.user(1, that.data.regex)
  },
  guanzu: function(e) {
    console.log(e)
    var authorid = e.currentTarget.dataset.authorid
    var index = e.currentTarget.dataset.index
    var care = that.data.care
    if (care[index].gz == 1) {
      console.log('11')
      care[index].gz = 0;
      that.setData({
        care: care
      })
    } else {
      console.log('22')
      care[index].gz = 1;
      that.setData({
        care: care
      })
    }
    console.log('data', that.data.care)
    that.modifyCare(authorid)
  },
  guanzu1: function(e) {
    console.log(e)
    var authorid = e.currentTarget.dataset.authorid
    var index = e.currentTarget.dataset.index
    var fans = that.data.fans
    if (fans[index].gz == 1) {
      console.log('11')
      fans[index].gz = 0;
      that.setData({
        fans: fans
      })
    } else {
      console.log('22')
      fans[index].gz = 1;
      that.setData({
        fans: fans
      })
    }
    console.log('data', that.data.fans)
    that.modifyCare(authorid)
  },
  modifyCare: function(authorID) {
    server.requestForm(false, 'put', 'user/modifyCare', {
        authorID: authorID,
        watcherID: app.globalData.userID
      },
      function(e) {
        console.log(e)
        wx.showToast({
          title: e.msg,
          icon: 'none',
          duration: 1000,
          mask: true
        });
      })
  },
  dj: function(e) {
    var dj = e.currentTarget.dataset.dd
    that.setData({
      dj: dj
    })
    that.user(dj,'^')
  },
  newsdetails: function(e) {
    console.log(e)
    var name = e.currentTarget.dataset.name
    var toid = e.currentTarget.dataset.toid
    var toheadpicpath = e.currentTarget.dataset.toheadpicpath
    server.requestForm(false, 'post', 'message/getConversationIDbyTalkerIDs', {
        toID: toid,
        fromID: app.globalData.userID
      },
      function(e) {
        console.log(e)
        if (e.result != null) {
          wx.navigateTo({
            url: '../newsdetails/newsdetails?name=' + name + '&toheadpicpath=' + toheadpicpath + '&toid=' + toid + '&conversationid=' + e.result,
          })
        } else {
          wx.navigateTo({
            url: '../newsdetails/newsdetails?name=' + name + '&toheadpicpath=' + toheadpicpath + '&toid=' + toid,
          })
        }
      })

  },
  addfriends: function(e) {
    wx.navigateTo({
      url: '../addfriends/addfriends',
    })
  },
  // onReachBottom: function (e) {
  //   console.log('that.data.hasNext', that.data.hasNext)
  //   if (that.data.hasNext) {

  //   }
  // },
})